var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var pay = require('../../../services/pay.js');

var app = getApp();
Page({
    data: {
        moneyList: [ '5','10','50','100','200','250','300','350','400','450','500','1000'],
        name: '',
        mobile: '',
        status: 0,
    },
     onLoad:  function(options) {
        this.getSettingsDetail();
    },
    mobilechange(e) {
        let mobile = e.detail.value;
        this.setData({
            mobile: mobile,
            status: 0
        });
        if (util.testMobile(mobile)) {
            this.setData({
                mobile: mobile,
                status: 1
            });
        }
    },
    recharge:function(e){
        let that = this;
        if (that.data.name == '' || that.data.mobile == ''){
            wx.showModal({
                title:'提示',
                content:'请填写姓名和手机',
                confirmText:'前往设置',
                success:function(res){
                if (res.confirm) {
                    wx.navigateTo({
                        url: '/packageA/ucenter/settings/index',
                    });
                 } else if (res.cancel) {

                  }
            }
        })
        }else{
            util.request(api.Recharge, {
                balance: e.currentTarget.id
            }, 'POST').then(function(res) {
                if (res.errno === 0) {
                    console.log(res);
                    let orderId = res.data.id
                    pay.recharge(parseInt(orderId)).then(res => {
                        wx.redirectTo({
                            url: '/pages/payResult/payResult?status=1&orderId=' + orderId
                        });
                    }).catch(res => {
                        wx.redirectTo({
                            url: '/pages/payResult/payResult?status=0&orderId=' + orderId
                        });
                    });
                }
            });
        }
    },
    bindinputName(event) {
        let name = event.detail.value;
        let mobile = this.data.mobile;
        this.setData({
            name: name,
        });
        if (util.testMobile(mobile)) {
            this.setData({
                status: 1
            });
        }
    },
    getSettingsDetail() {
        let that = this;
        util.request(api.SettingsDetail).then(function(res) {
            if (res.errno === 0) {
                that.setData({
                    name: res.data.name,
                    mobile: res.data.mobile,
                });
            }
        });
    },
    saveInfo() {
        let name = this.data.name;
        let mobile = this.data.mobile;
        let status = this.data.status;
        if (name == '') {
            util.showErrorToast('请输入姓名');
            return false;
        }
        if (mobile == '') {
            util.showErrorToast('请输入手机号码');
            return false;
        }
        let that = this;
        util.request(api.SaveSettings, {
            name: name,
            mobile: mobile,
        }, 'POST').then(function(res) {
            if (res.errno === 0) {
                util.showErrorToast('保存成功');
                wx.navigateBack()
            }
        });
    },
})